// pages/profile/order/order.js
import {
  getOrders, updateState
} from "../../apis/order_api";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    state: 0,
    isOpen: "",
    orderList: []
  },
  userData: {
    code: "",
    phone: "13697003363",
    page: 0,
    total: 1,
    points: 0
  },
  changeOpen(e) {
    let isOpen = e.currentTarget.dataset.id
    if (isOpen == this.data.isOpen) {
      this.setData({
        isOpen: ""
      })
    } else {
      this.setData({
        isOpen: isOpen
      })
    }
  },
  changeState(e) {
    let state = e.currentTarget.dataset.state
    if (state != this.data.state) {
      this.userData.page = 0
      this.setData({
        orderList: [],
        state: state
      })
      this.getOrder()
    }
  },
  getOrder() {
    let phone = this.userData.phone
    let page = this.userData.page + 1
    let total = this.userData.total
    let orderList = this.data.orderList
    let state = this.data.state
    if (orderList.length < total) {
      getOrders(page, 10, phone, state).then(res => {
        if (res.success) {
          total = res.data.total ? res.data.total : 1
          let list = res.data.records
          list.forEach(order => {
            let product = {
              "all": 0,
              "name": "总计",
              "number": 0,
              "price": "",
            }
            order.productList.forEach(product => {
              product.all = (product.price * product.number)
            })
            product.number = order.productList.reduce((prev, cur) => {
              return prev + cur.number;
            }, 0)
            product.all = order.productList.reduce((prev, cur) => {
              return prev + cur.all;
            }, 0)
            order.productList.push(product)
          })
          orderList = orderList.concat(list)
          this.setData({
            orderList: orderList
          })
          this.userData.total = total
          this.userData.page = page
        } else {
          console.log(res.message);
        }
      })
    }
  },
  // 取消订单
  cancelOrder(e) {
    let id = e.currentTarget.dataset.id
    let total = e.currentTarget.dataset.total
    let phone = this.userData.phone
    let that = this
    wx.showModal({
      title: '提示',
      content: '取消订单只返还90%的积分,确认是否取消?',
      success(res) {
        if (res.confirm) {
          updateState(id, 4, phone, total).then(res => {
            if (res.code == 200) {
              // 刷新上一个页面数据并跳转
              let pages = getCurrentPages();
              let releaseList = pages[pages.length - 2];
              releaseList.getPageData()
              wx.navigateBack({
                delta: 1
              })
              wx.showToast({
                title: "已返还" + parseInt(total * 0.9) + "积分",
                icon: 'success',
                duration: 2000
              })
            } else {
              wx.showToast({
                title: res.message,
                icon: 'error',
                duration: 2000
              })
            }
          })
        }
      }
    })

  },
  // 重新下单
  reOrder(e) {
    let id = e.currentTarget.dataset.id
    let total = e.currentTarget.dataset.total
    let phone = this.userData.phone
    let points = this.userData.points
    if (points >= total) {
      wx.showModal({
        title: '提示',
        content: '确认重新下单?',
        success(res) {
          if (res.confirm) {
            updateState(id, 1, phone, total).then(res => {
              if (res.code == 200) {
                // 刷新上一个页面数据并跳转
                let pages = getCurrentPages();
                let releaseList = pages[pages.length - 2];
                releaseList.getPageData()
                wx.navigateBack({
                  delta: 1
                })
                wx.showToast({
                  title: "下单成功",
                  icon: 'success',
                  duration: 2000
                })
              } else {
                wx.showToast({
                  title: res.message,
                  icon: 'error',
                  duration: 2000
                })
              }
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: "积分不足",
        icon: 'error',
        duration: 2000
      })
    }


  },
  // 删除订单
  delOrder(e) {
    let id = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确认删除该订单?',
      success(res) {
        if (res.confirm) {
          updateState(id, 5, "", "").then(res => {
            if (res.code == 200) {
              // 刷新上一个页面数据并跳转
              let pages = getCurrentPages();
              let releaseList = pages[pages.length - 2];
              releaseList.getPageData()
              wx.navigateBack({
                delta: 1
              })
              wx.showToast({
                title: "删除成功",
                icon: 'success',
                duration: 2000
              })
            } else {
              wx.showToast({
                title: res.message,
                icon: 'error',
                duration: 2000
              })
            }
          })
        }
      }
    })
  },
  // 商家信息按钮
  // onButtonClick() {
  //   // 处理按钮点击事件的逻辑
  //   wx.redirectTo({
  //     url: '/pages/Information/Information', // 跳转到目标页面的路径
  //   });
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '我的订单'
    })
    this.userData.points = wx.getStorageSync("points") ? wx.getStorageSync('points') : 136
    this.userData.phone = wx.getStorageSync('phoneNumber') ? wx.getStorageSync('phoneNumber') : "13697003363"
    this.getOrder()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
